home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15145 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: newshost.centrum.is!news
  2. From: bjarnir@centrum.is (Bjarni Ragnarsson)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: HELP with a simple C Structure
  5. Date: 3 Apr 1996 15:37:50 GMT
  6. Organization: BR Software
  7. Message-ID: <4ju60e$lsj@newshost.centrum.is>
  8. References: <4jm38u$j1a@news.bellglobal.com>
  9. NNTP-Posting-Host: tungl-100.centrum.is
  10. X-Newsreader: WinVN version 0.82
  11.  
  12. In article <4jm38u$j1a@news.bellglobal.com>, bogdanfl@aei.ca (Bogdan  Florescu) says:
  13. >
  14. >I need to do this, and I don't khow how.
  15. >
  16. >struct Employee
  17. >        {
  18. >        float salary;
  19. >        char department;
  20. >        }
  21. >
  22. >Employee Jim, Paul, Pat, Scott;
  23. >
  24. >void main(void)
  25. >        {
  26. >        Jim.salary=15;
  27. >        Paul.salary=14;
  28. >        Pat.salary=13;
  29. >        Scott.salary=12;
  30. >
  31. >        Jim.department='a';
  32. >        Paul.department='b';
  33. >        Pat.department='c';
  34. >        Scott.department='b';
  35. >        ................................
  36. >        }
  37. >
  38. >I need to replace the dots with a program that tells me who else works
  39. >in Paul's department and how much money he makes.
  40. >
  41. >I would like to have the structure variables named with the name of
  42. >the employee, (instead of an array with the name of the employee as a
  43. >member) so I can easily access their members. The structure can be
  44. >modified, but I really need to be able to have expressions such as
  45. >Jim.salary.
  46. >
  47. >Thank you,
  48. >Bogdan Florescu
  49.  
  50. Can't see how you could do that unless you wan't to check each struct-variable
  51. with an "if" statement for instance.
  52. Example:
  53.     if (Jim.department == Paul.department)
  54.         printf("Jim\n");     //or whatever you want to do with people 
  55.                 //working in same department.
  56.     if (Pat.department==Paul.department)
  57.         printf("Pat\n");
  58.  
  59. ...etc.
  60.  
  61. I think you have to define the variables as an array if you wan't to do this in a more
  62. general way.   Then you would include a name field in the struct.
  63.     // d is Pauls department in this example.
  64.     for (i=0;i<max;i++) 
  65.         if (EmployeArray[i].department == d )
  66.             printf("%s\n",EmployeArray[i].name);
  67.  
  68. or something like that.
  69.  
  70. If you wish to refer to specific employes by name (in your code), you could always
  71. define a number for each employe in the array.
  72.  
  73.     enum EmployeNumber {Jim=0,Paul,Pat,Scott};
  74.  
  75. and refer to an employe as 
  76.     EmployeArray[Paul].department
  77.     EmployeArray[Paul].salary
  78.  
  79. etc.
  80.  
  81. A bit messy but ......
  82.  
  83. Maybe I misunderstood your question in the first place ?
  84.  
  85. Bjarni Ragnarsson
  86.